#include "vppacketqueue.h"

void VpPacketQueue::setSerial(int newSerial)
{
    m_serial = newSerial;
}

int VpPacketQueue::get_size()
{
    return m_list.size();
}

int VpPacketQueue::serial() const
{
    return m_serial;
}

int VpPacketQueue::stream_idx() const
{
    return m_stream_idx;
}

void VpPacketQueue::setStream_idx(int newStream_idx)
{
    m_stream_idx = newStream_idx;
}

void VpPacketQueue::stop()
{
    m_stop = true;
}

void VpPacketQueue::start()
{
    m_stop = false;
}

VpPacketQueue::VpPacketQueue()
{
    m_serial     = 0;
    m_stream_idx = 0;
    m_stop       = false;
    m_codec_ctx  = nullptr;
}

VpPacketQueue::~VpPacketQueue()
{
    flush_queue();
}

void VpPacketQueue::push(VpPacket &elem)
{
    lock_guard<mutex> lock(m_mutex);
    m_list.push_back(elem);
}

VpPacket VpPacketQueue::pop()
{
    VpPacket res;
    unique_lock<mutex> lock(m_mutex);
    while(m_list.empty() && !m_stop)
        m_cond.wait_for(lock, chrono::milliseconds(10));
    if(m_stop) {
        lock.unlock();
        return res;
    }
    res = m_list.front();
    m_list.pop_front();
    return res;
}

void VpPacketQueue::flush_queue()
{
    lock_guard<mutex> lock(m_mutex);
    while(!m_list.empty())
        m_list.pop_front();
}
